package jcrweb.as3
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.utils.setTimeout;
	import mx.controls.Alert;
	public class LanguageUtil
	{
		public static var language:String = null;
		
		private static var zhCNXml:XML = null;
		private static var enUSXml:XML = null;
		private static var jaJPXml:XML = null;
		
		private static var util:LanguageUtil = null;

		private function init():void{		
			if(language == null){
				var loginUtil:LoginUtil = new LoginUtil();
				loginUtil.loginCheck();
			}
			loadLanguageData();
		}
		public static function loadLanguageData():void{
			if(zhCNXml == null){
				RequestUtil.sendRequest(RequestUtil.LOCALE_URL+"/zh_CN.xml",Event.COMPLETE,completeZh,false);
				RequestUtil.sendRequest(RequestUtil.LOCALE_URL+"/en_US.xml",Event.COMPLETE,completeEn,false);
				RequestUtil.sendRequest(RequestUtil.LOCALE_URL+"/ja_JP.xml",Event.COMPLETE,completeJa,false);			
			}
		}
		private function lan():void{
		
		}
		public static function getInstance():LanguageUtil{
			if(util!=null){
				return util;
			}
			language = LoginUtil.getLoginLanguage();
			util = new LanguageUtil();
			util.init();
			return util;
		}
		public function getData(key:String):String{
			if(language == "zh_CN"){
				return zhCNXml.child(key).toString();
			}else if(language == "en_US"){
				return enUSXml.child(key).toString();
			}else if(language == "ja_JP"){
				return jaJPXml.child(key).toString();
			}else{
				return zhCNXml.child(key).toString();
			}
		}
		private static function completeZh(event:Event):void{
			language = LoginUtil.getLoginLanguage();
			var loader:URLLoader = URLLoader(event.target); 
			zhCNXml = new XML(loader.data);
		}
		private static function completeEn(event:Event):void{
			language = LoginUtil.getLoginLanguage();
			var loader:URLLoader = URLLoader(event.target); 
			enUSXml = new XML(loader.data);
		}
		private static function completeJa(event:Event):void{
			language = LoginUtil.getLoginLanguage();
			var loader:URLLoader = URLLoader(event.target); 
			jaJPXml = new XML(loader.data);
		}
	}
}